﻿using InChatter.Client.Service;
using InChatter.Client.WPFClient.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace InChatter.Client.WPFClient.View
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        #region 单例
        private static MainWindow _Instance;
        internal static MainWindow Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new MainWindow();
                }
                return _Instance;
            }

        }
        MainWindowViewModel model;
        private MainWindow()
        {
            InitializeComponent();
            this.model = new MainWindowViewModel();
            this.DataContext = model;
            SetCheck();
            model.MessageItemChanged = new Action(MessageItemsChanged);
            model.RequestToChat = new Action(model_RequestToChat);
        }
        #endregion
        private void model_RequestToChat()
        {
            if (this.Visibility != Visibility.Visible)
            {
                this.Visibility = Visibility.Visible;
            }
            this.Activate();
        }
        private void MessageItemsChanged()
        {
            if (this.lbMessage.Items.Count > 0)
            {
                this.lbMessage.ScrollIntoView(this.lbMessage.Items[this.lbMessage.Items.Count - 1]);
            }
        }

        #region Event
        private void SendMsg()
        {
            TextRange a = new TextRange(rtbSend.Document.ContentStart, rtbSend.Document.ContentEnd);
            model.SendMsgCommand.Execute(a.Text);
            a.Text = "";
            rtbSend.Focus();
        }
        private void SetCheck()
        {
            if (string.IsNullOrEmpty(MessageConfig.Instance.SendMode))
            {
                MessageConfig.Instance.SendMode = "Enter";
            }
            foreach (var item in spMode.Children)
            {
                RadioButton btn = item as RadioButton;
                if (btn.Tag.ToString() == MessageConfig.Instance.SendMode)
                {
                    btn.IsChecked = true;
                    break;
                }
            }
        }

        private void rtbSend_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (MessageConfig.Instance.SendMode == "Enter")
            {
                e.Handled = true;
                if (e.Key == Key.Enter && e.KeyboardDevice.Modifiers == ModifierKeys.None)
                {
                    SendMsg();
                }
            }
            else if (MessageConfig.Instance.SendMode == "Ctrl+Enter")
            {
                if (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.Enter)
                {
                    e.Handled = true;
                    SendMsg();
                }
            }
        }
        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            MessageConfig.Instance.SendMode = ((RadioButton)sender).Tag.ToString();
            ThreadPool.QueueUserWorkItem((obj) =>
            {
                MessageConfig.SaveConfig();
            });
        }
        private void ChangeMode_Click(object sender, RoutedEventArgs e)
        {
            popMode.IsOpen = true;
        }
        private void Send_Click(object sender, RoutedEventArgs e)
        {
            SendMsg();
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Visibility = System.Windows.Visibility.Collapsed;
        }
        #endregion
    }
}
